package com.example.Servlet;

import com.alibaba.fastjson.JSONArray;
import com.example.Function.DataBaseOp;
import com.example.Object.Question;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Echo
 * 手动输入题目 （没做完）
 */
@WebServlet("/ReceiveHandFile")
public class ReceiveHandFile extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String title = req.getParameter("title");
        String data = req.getParameter("data");

        /**
         * 前端需要先检查是否重名
         */


        DataBaseOp.creatTable(title);
        System.out.println(title);

        System.out.println("empjson字符串" + data);
        List<Question> questions = JSONArray.parseArray(data, Question.class);
       DataBaseOp.insertQuestion(questions,title);
        System.out.println(questions);

        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        out.println(gson.toJson(true));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
